#include "Lexer.h"
#include "CharCheck.h"



Lexer::Lexer()
{
    //ctor
    ptoken=0;
    buff=0;
    size=0;
    ptable=0;
}

Lexer::~Lexer()
{
    //dtor
}

void Lexer::setBuffer(char *buff,int size)
{
    this->buff=buff;
    this->size=size;
    if(ptoken==0){
        ptoken=new Token();
    }
    ptoken->line=0;
    ptoken->type=Token_None;
    ptoken->pstr=0;
    state=SLexerBegin;
    ebuff=this->buff+size;
    ////////////////////////////////
    if(ptable==0){
        ptable=STable::getInstance();
    }
}


Token *Lexer::getKeyToken()
{
    Token *token;
    token=getToken();
    if(token){
        switch(token->type){
            case 0:{
                return getKeyToken();
            }
            default:{
                break;
            }
        }
    }
    return token;
}

Token *Lexer::getToken()
{
    while(buff!=ebuff){
        switch(state){
            case SLexerBegin:{
                if(' '==*buff){
                    break;
                }
                if('\n'==*buff){
                    ptoken->line++;
                    break;
                }
                if(charCheckLetter(*buff)){
                    //表示读取变量
                    ptoken->type=Token_Variable;
                    state=SLexerVariable;
                    tokenlen=1;
                    tokenstr=buff;
                    break;
                }
                if('0'==*buff){

                }
                //表示整数或者浮点数
                if(charCheckDigit(*buff)){

                }
                if('"'==*buff){
                    state=SLexerString;
                    ptoken->type=Token_String;
                    tokenlen=0;
                    tokenstr=buff+1;
                    break;
                }
                if('\''==*buff){
                    state=SLexerChar;
                    ptoken->type=Token_Char;
                    tokenlen=0;
                    tokenstr=buff+1;
                    break;
                }

            }
            case SLexerVariable:{
                if(charCheckLetter(*buff)||charCheckDigit(*buff)){
                    tokenlen++;
                    break;
                }
                ptoken->pstr=ptable->pushString(tokenstr,tokenlen);
                ptoken->checkKey();
                return ptoken;
            }
            case SLexerString:{
                if('"'==*buff){
                    state=SLexerBegin;
                    ptoken->pstr=ptable->pushString(tokenstr,tokenlen);
                    buff++;
                    return ptoken;
                }
                if('\\'==*buff){
                    state=SLexerStringStat;
                    tokenlen++;
                    break;
                }
                tokenlen++;
                break;
            }
            case SLexerStringStat:{
                state=SLexerString;
                tokenlen++;
                break;
            }
             case SLexerChar:{
                if('\''==*buff){
                    state=SLexerBegin;
                    ptoken->pstr=ptable->pushString(tokenstr,tokenlen);
                    buff++;
                    return ptoken;
                }
                if('\\'==*buff){
                    state=SLexerCharStat;
                    tokenlen++;
                    break;
                }
                tokenlen++;
                break;
            }
            case SLexerCharStat:{
                state=SLexerChar;
                tokenlen++;
                break;
            }
            default:{
            }
        }
        buff++;

    }
}
